//模块主窗口
AuthorManagerPanel = Ext.extend(Ext.Panel,{
	 closable: true,
	 layout:'border',
   	 autoScroll:true,
   	 width:'100%',
   	 initComponent : function() {
   			//this.tbar =  this.buildBbar() ;
   			var thisPanel = this;
   			
			var formpanel = new Ext.form.FormPanel({
				text : "表单",
				layout : "form",
				region : "center",
				labelAlign : "right",
				frame : true,
				width : '100%',
				items : [],
				buttonAlign :'center',
				minButtonWidth:60,
				tbar:[{
					ref:'../addBut',
					text:"添加子节点",
					disabled :true,
					handler:thisPanel.addForm,
					iconCls : 'ico-task-add'
				},'-',{
					ref:'../deleBut',
					text:"删除",
					disabled :true,
					handler:thisPanel.deletePage,
					iconCls : 'ico-task-delete'
				},'-',{
					ref:'../editBut',
					text:"编辑节点",
					disabled :true,
					handler:thisPanel.editForm,
					iconCls : 'ico-task-edit'
				},'-',{
					ref:'../moveBut',
					text:"移动节点",
					disabled :true,
					handler:thisPanel.moveNode,
					iconCls : 'ico-task-edit'
				}]
				});
			
	    	var treePanel = new Ext.tree.TreePanel ({
						 width : "20%",
						 header :false,
					     split: true,
						// rootVisible : false,
						 region : "west",
						 autoScroll : true,
						 loader :  new Ext.tree.TreeLoader({
    							url : 'user_geAllPage.action',
    							root:'root'
    						}),
						 root : new Ext.tree.AsyncTreeNode({
							id:'0',  
					        text: '所有资源' ,
					        leaf:false,
					        expended:true
						 }),
						 region : "west",
						 listeners : {
						    click : function(n, e) {
	    							this.selectNode = n;
	    							var set ;
	    							formpanel.remove(formpanel.items.get(0));
	    							formpanel.deleBut.setDisabled(false);
	    							formpanel.editBut.setDisabled(false);
	    							formpanel.moveBut.setDisabled(false);
	    							if(n.id==0)  {
	    								formpanel.deleBut.setDisabled(true);
	    								formpanel.editBut.setDisabled(true);
	    								formpanel.moveBut.setDisabled(true);
	    							}
	    							//var isExpand = n.expanded?"展开":"不展开";
									if(!n.leaf) {
										formpanel.addBut.setDisabled(false);
										set  = new Ext.form.FieldSet ({
								    		collapsible: true,
								    		titleCollapse:true,
								            title: n.text,
								            autoHeight:true,
								            defaultType: 'textfield',
								            defaults: {
										        bodyStyle:'padding:3px'
										    },items:[{
								            	xtype:'textfield',fieldLabel:"名称",value:n.text,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"类型",value:n.attributes.type,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"是否生效",value:n.attributes.isVisible,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"是否展开",value:n.attributes.expTag,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"图标样式",value:n.attributes.iconCls,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"优先级",value:n.attributes.priority,readOnly:true,anchor:"90%"
								            }]
										     });
									} else {
										formpanel.addBut.setDisabled(true);
										set  = new Ext.form.FieldSet ({
								    		collapsible: true,
								    		titleCollapse:true,
								            title: n.text,
								            autoHeight:true,
								            defaults: {
										        bodyStyle:'padding:5px'
										    },items:[{
								            	xtype:'textfield',fieldLabel:"名称",value:n.text,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"类型",value:n.attributes.type,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"是否生效",value:n.attributes.isVisible,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"图标样式",value:n.attributes.iconCls,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"优先级",value:n.attributes.priority,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"对象名称",value:n.attributes.objectName,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"资源URL",value:n.attributes.jsSource,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"初始化回调函数",value:n.attributes.afterLoader,readOnly:true,anchor:"90%"
								            },{
								            	xtype:'textfield',fieldLabel:"DeBug模式",value:n.attributes.debug,readOnly:true,anchor:"90%"
								            }]
										     });
									}
									formpanel.add(set);
									formpanel.doLayout();
									return true;
							},
					 		contextmenu : function(n, e) {
										
							}
					      }
					 }) ;
	    	formpanel.addFormPanel =  new Ext.Window({
	   			layout:"fit",
	   			title: "添加资源",
	   			closeAction: 'hide',
				resizable:false,
				modal:true,
				width:550,
				height:375,
				items :[new AddForm({tree:treePanel})]
   			});
	    	
	    	formpanel.MoveNodeFormPanel =  new Ext.Window({
	   			layout:"fit",
	   			title: "编辑资源",
	   			closeAction: 'hide',
				resizable:false,
				modal:true,
				width:350,
				height:100,
				items :[new MoveNodeForm({tree:treePanel})]
   			});
	    	treePanel.root.reload();
	    	
			this.items = [treePanel,formpanel] ;
   		AuthorManagerPanel.superclass.initComponent.call(this);
   	},
//	bbar:[{
//		xtype:"label",text:"信息"
//	}],
   	addForm : function(btn) {
   		//alert(btn.text)
   		var tform = btn.ownerCt.ownerCt.addFormPanel;
   		var form = tform.items.get(0).getForm() ;
   		tform.setTitle("添加资源");
   		var n = form.tree.selectNode;
   	    tform.show();
   	    form.reset();
		form.findField("tdPageMain.parentId").setValue( n.id) ;
		form.findField("isHiden").setValue(false) ;
   	     
   	},
   	deletePage: function(btn) {
   		var tform = btn.ownerCt.ownerCt.addFormPanel;
   		var form = tform.items.get(0).getForm() ;
   		var n = form.tree.selectNode;
   		//alert(n.id);
   		var title = "确认要删除 “"+n.text+"” 吗?";
   		Ext.MessageBox.confirm("确认", title, function(btn) {
					if (btn == "yes") {
						Ext.Ajax.request({
							waitMsg : '正在执行  ',
							method : 'POST',
							params : {
								"tdPageMain.id":n.id 
							},
							url : 'main_deleteMainPageById.action',
							scope : this,
							success : function(request) {
								n.parentNode.reload(function(node){
									node.fireEvent('click',node);
								});
								
							},
							failure : function() {
								//Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
								MsgTip.msg({
								 	msg:"删除失败~",
								 	cmp: btn.ownerCt.ownerCt,
								 	pos:"center"
								 });
							}
						});
					}
				});
   	},
   	moveNode:function(btn) {
   		var tform = btn.ownerCt.ownerCt.MoveNodeFormPanel;
   		var form = tform.items.get(0).getForm() ;
   		var n = form.tree.selectNode;
   		tform.show();
   		form.findField("tdPageMain.id").setValue( n.id) ;
   	},
   	editForm:function(btn) {
   		var tform = btn.ownerCt.ownerCt.addFormPanel;
   		var form = tform.items.get(0).getForm() ;
   		form.reset(); 
   		
   		var n = form.tree.selectNode;
   		tform.setTitle("编辑资源");
   	    tform.show();
   	    form.load({                    
                    waitMsg : '正在加载数据请稍后',//提示信息                
                    waitTitle : '提示',//标题                
                    url : 'main_getObjectById.action',            
                    params:{
   	    				ids:n.id
   	    			},                
                    method:'post',              
                    success:function(form,action){
   	    				var obj = action.result.root[0];
                         form.setValues({
                        	 'tdPageMain.id':obj.id,
                        	 'tdPageMain.text':obj.text,
                        	 'tdPageMain.parentId':obj.parentId,
                        	 'tdPageMain.isLeaf':obj.isLeaf,
                        	 'tdPageMain.isExpanded':obj.isExpanded,
                        	 'tdPageMain.priority':obj.priority,
							 'tdPageMain.iconCls':obj.iconCls,
                        	 'tdPageMain.type':obj.type,
                        	 'tdPageMain.objectName':obj.objectName,
                        	 'tdPageMain.urlSource':obj.urlSource,
                        	 'tdPageMain.afterLoader':obj.afterLoader,
                        	 'tdPageMain.isVisible':obj.isVisible,
                        	 'tdPageMain.isDebug':obj.debug==null?'0':obj.debug,
                        	 'isHiden':true
                         });
                    }
   	    			,
                    failure:function(form,action){ 
                    	
                    }            
        });
   	},
   	buildBbar: function() {
   		return [{
   				text: "添加资源",
   				handler:this.addForm,
   				iconCls: 'ico-task-hf_08'
   			}] ;
   	}
});


AddForm = Ext.extend(Ext.form.FormPanel,{
	layout:"form",
	region : "center",
	labelAlign:"right",
	width:550,
	height:300,
	frame:true,
	initComponent: function(){
	this.items=[{fieldLabel : '资源名称',
					name : 'tdPageMain.text',
					xtype : 'textfield',
					allowBlank:false,
					anchor:"90%",
					maxLength:20,
					maxLengthText:"最大长度不能超过20个字符",
					minLength:2,
					minLengthText:"最少要2个字符"
				},{fieldLabel : '类型',
					name : 'tdPageMain.type',
					allowBlank:false,
					xtype : 'combo',
					hiddenName:'tdPageMain.type',
					editable : false,
					mode : 'local',
					triggerAction : 'all',
					store : [['1','视图资源'],['2','权限'],['4','一级菜单'],['3','二级菜单']],
					value:'1',
					anchor:"90%"
				},{fieldLabel : '是否生效',
					name : 'tdPageMain.isVisible',
					allowBlank:false,
					xtype : 'combo',
					hiddenName:'tdPageMain.isVisible',
					editable : false,
					mode : 'local',
					triggerAction : 'all',
					store : [[true,'是'],[false,'不生效']],
					value:'1',
					anchor:"90%"
				},{fieldLabel : '是否为叶子节点',
					name : 'tdPageMain.isLeaf',
					xtype : 'combo',
					editable : false,
					hiddenName:'tdPageMain.isLeaf',
					mode : 'local',
					triggerAction : 'all',
					store : [[true,'是叶子节点'],[false,'不是叶子节点']],
					value:false,
					anchor:"90%"
				},{fieldLabel : '是否展开',
					name : 'tdPageMain.isExpanded',
					allowBlank:false,
					xtype : 'combo',
					hiddenName:'tdPageMain.isExpanded',
					editable : false,
					mode : 'local',
					triggerAction : 'all',
					store : [[true,'展开'],[false,'不展开']],
					value:false,
					anchor:"90%"
				},{fieldLabel : 'Debug模式',
					name : 'tdPageMain.isDebug',
					allowBlank:false,
					hiddenName:'tdPageMain.isDebug',
					xtype : 'combo',
					editable : false,
					hidden:true,
					mode : 'local',
					triggerAction : 'all',
					store : [[false,'关闭'],[true,' 打开']],
					value:false,
					anchor:"90%"
				},{fieldLabel : '图标样式',
					name : 'tdPageMain.iconCls',
					xtype : 'textfield',
					anchor:"90%",
					maxLength:30,
					maxLengthText:"最大长度不能超过30个字符",
					minLength:5,
					minLengthText:"最少要5个字符"
				},{fieldLabel : '优先级',
					name : 'tdPageMain.priority',
					xtype : 'textfield',
					anchor:"90%",
					vtype:'number1',
					maxLength:4,
					maxLengthText:"最大长度不能超过4个字符"
				},{fieldLabel : '对象名称',
					name : 'tdPageMain.objectName',
					xtype : 'textfield',
					anchor:"90%",
					maxLength:50,
					maxLengthText:"最大长度不能超过50个字符"
				},{fieldLabel : '资源URL',
					name : 'tdPageMain.urlSource',
					xtype : 'textfield',
					anchor:"90%",
					maxLength:100,
					maxLengthText:"最大长度不能超过100个字符"
				},{fieldLabel : '初始化回调函数',
					name : 'tdPageMain.afterLoader',
					xtype : 'textfield',
					anchor:"90%",
					maxLength:50,
					maxLengthText:"最大长度不能超过50个字符"
				},{
					boxLabel:"完成后自动关闭",
					name : 'isHiden',
					xtype : 'checkbox'
				},{ 
					name : 'tdPageMain.parentId',
					xtype : 'textfield',
					hidden:true,
					value:"0"
				},{ 
					name : 'tdPageMain.id',
					xtype : 'textfield',
					hidden:true
				}
		];
	  AddForm.superclass.initComponent.call(this);
	},
	buttonAlign :'center',
	minButtonWidth:60,
	buttons:[{
		text:"确定",
		handler : function(btn){
		     var thisForm = btn.ownerCt.ownerCt.getForm();
		     var tree = btn.ownerCt.ownerCt.tree.selectNode;
		     if(!thisForm.isValid()) {
		    	 return ;
		     }
		     var isHid = thisForm.findField("isHiden").getValue();
			 thisForm.submit({
					waitTitle : '请稍候...',
					waitMsg : '正在提交表单数据,请稍候...',
					method : 'post',
					url:'main_addMainPage.action',
					success : function(form, action) {
				 		var id = thisForm.findField('tdPageMain.id').getValue();
				  		if(id && id!="") {
				  			tree.parentNode.reload(function(node){
				  				node.fireEvent('click',node.findChild('id',id));
				  			});
				  		}else{
				  			tree.reload();
				  		}
				  		if(isHid)
				  			btn.ownerCt.ownerCt.ownerCt.hide();
					}
		    });
		    // btn.ownerCt.ownerCt.ownerCt.hide();//隐藏弹出页面
		}
	}, {
		text : '重置',
		handler : function() {this.ownerCt.ownerCt.form.reset();}
	}, {
		text : '取消',
		handler : function() {this.ownerCt.ownerCt.ownerCt.hide();}
	}]
});

MoveNodeForm = Ext.extend(Ext.form.FormPanel,{
	layout:"form",
	region : "center",
	labelAlign:"right",
	width:350,
	height:100,
	frame:true,
	initComponent: function(){
	this.items=[{ 
					passName : 'tdPageMain.parentId',
					xtype : 'treecombo',
					fieldLabel : '移动到',
					resizable :true,
					allowBlank : false,
					treeHeight:350,
					allowUnLeafClick : false,
					tree : new Ext.tree.TreePanel({
		            rootVisible : true,
		            root : new Ext.tree.AsyncTreeNode({
		                id : '0',
		                text : "所有资源",
		                expanded : true,
		                loader : new Ext.tree.TreeLoader({
		                    dataUrl : "user_geAllPage.action?type=root",
		                    root:'root'
		                })
		            })
		        })
				},{ 
					name : 'tdPageMain.id',
					xtype : 'textfield',
					allowBlank : false,
					hidden:true
				}
		];
	  MoveNodeForm.superclass.initComponent.call(this);
	},
	buttonAlign :'center',
	minButtonWidth:60,
	buttons:[{
		text:"确定",
		handler : function(btn){
		     var thisForm = btn.ownerCt.ownerCt.getForm();
		     var tree = btn.ownerCt.ownerCt.tree.selectNode;
		     if(!thisForm.isValid()) {
		    	 return ;
		     }
			 thisForm.submit({
					waitTitle : '请稍候...',
					waitMsg : '正在提交表单数据,请稍候...',
					method : 'post',
					url:'main_moveNode.action',
					success : function(form, action) {
//				 		var id = thisForm.findField('mainPage.id').getValue();
//				 		
//				  		if(id && id!="") {
//				  			tree.parentNode.reload(function(node){
//				  				node.fireEvent('click',node);
//				  			});
//				  		}else{
//				  			tree.reload();
//				  		}
						var node = tree.parentNode ;
				 		while(node.id && node.id!=0) {
				 			node = node.parentNode ;
				 		}
				 		node.reload();
					}
		    });
		     btn.ownerCt.ownerCt.ownerCt.hide();//隐藏弹出页面
		}
	}, {
		text : '取消',
		handler : function() {this.ownerCt.ownerCt.ownerCt.hide();}
	}]
});